package superminer.xinfang.com.ipreport.view;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.EditText;

/**
 * Created by Administrator on 2019/3/6.
 */

public class MyEditText extends EditText {
    String str="";
    public MyEditText(Context context, AttributeSet attrs) {
        super(context, attrs, android.R.attr.editTextStyle);
        init();
    }

    public MyEditText(Context context, AttributeSet attrs, int defStyle){
        super(context, attrs, defStyle);
        init();
    }

    public MyEditText(Context context) {
        super(context);
        init();
    }

   private void init()
   {
//       setText("0");
       setSelection(this.getText().toString().length());//将光标移至文字末尾
       addTextChangedListener(new TextWatcher() {
           @Override
           public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {


           }

           @Override
           public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

               str=charSequence.toString();

           }

           @Override
           public void afterTextChanged(Editable editable) {
               Log.d("==2",str.indexOf("0")+"");
               if(str.length()>1&&str.indexOf("0")==0)
               {
                   setText(str.substring(1,str.length()));
                   setSelection(str.length());//将光标移至文字末尾
               }

               //只能输入三位数字
               if (str.length()==4&&str.indexOf(".")!=3)
               {
                   setText(str.substring(0,3));
                   setSelection(str.length());//将光标移至文字末尾
               }
//               else if(str.length()==0)
//               {
//                   setText("0");
//                   setSelection(str.length());//将光标移至文字末尾
//               }


           }
       });
   }


}

